package com.mysoft.gfyx.workbench.controller;


import com.mysoft.framework.context.api.ContextManager;
import com.mysoft.framework.context.info.ContextInfo;
import com.mysoft.framework.rpc.annotation.PubAction;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.contants.RequestPrefix;
import com.mysoft.gfyx.common.util.PageUtil;
import com.mysoft.gfyx.workbench.controller.dto.SetToDoneOrReadRequestDto;
import com.mysoft.gfyx.workbench.controller.dto.TaskWakeHistoryResponseDto;
import com.mysoft.gfyx.workbench.controller.dto.TaskWakeResponseDto;
import com.mysoft.gfyx.workbench.remoteservice.RemoteSystemMessageService;
import com.mysoft.gfyx.workbench.remoteservice.dto.MessageQueryFilter;
import com.mysoft.gfyx.workbench.remoteservice.dto.MessageSetReadResponseDto;
import com.mysoft.gfyx.workbench.remoteservice.dto.TaskWakeDto;
import com.mysoft.gfyx.workbench.remoteservice.dto.TaskWakeHistoryDto;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Objects;
import java.util.UUID;

@PubService(value = "/messageDataApi", prefix = RequestPrefix.API)
@Tag(name = "消息控制器-API")
public class MessageDataApiController {

    @Autowired
    private RemoteSystemMessageService remoteSystemMessageService;

    @Autowired
    private ContextManager contextManager;

    @Autowired
    private PageUtil pageUtil;

    @PubAction(value = "/queryTaskWakeHistoryList", method = RequestMethod.POST)
    public TaskWakeHistoryResponseDto queryTaskWakeHistoryList(@RequestBody MessageQueryFilter queryFilter) {
        ContextInfo contextInfo = contextManager.fetchContextInfo();
        UUID userId = contextInfo.getUserContext().getUserId();
        queryFilter.setUserGuid(userId.toString());
        queryFilter.setOrderBy("desc");
        ArrayList<TaskWakeHistoryDto> list = remoteSystemMessageService.QueryTaskWakeHistoryList(queryFilter);
        int total = remoteSystemMessageService.QueryTaskWakeHistoryCount(queryFilter);

        TaskWakeHistoryResponseDto responseDto = new TaskWakeHistoryResponseDto();
        list.forEach(message -> {
            String pageUrl = pageUtil.getPageUrlBusinessType(message.getBusinessType());
            if (!Objects.equals(pageUrl, "")) {
                message.setPageUrl(pageUrl);
            }
        });

        responseDto.setList(list);
        responseDto.setTotal(total);
        return responseDto;
    }

    @PubAction(value = "/queryTaskWakeList", method = RequestMethod.POST)
    public TaskWakeResponseDto queryTaskWakeList(@RequestBody MessageQueryFilter queryFilter) {
        ContextInfo contextInfo = contextManager.fetchContextInfo();
        UUID userId = contextInfo.getUserContext().getUserId();
        queryFilter.setUserGuid(userId.toString());
        queryFilter.setOrderBy("desc");
        ArrayList<TaskWakeDto> list = remoteSystemMessageService.QueryTaskWakeList(queryFilter);
        int total = remoteSystemMessageService.QueryTaskWakeCount(queryFilter);

        TaskWakeResponseDto responseDto = new TaskWakeResponseDto();
        list.forEach(message -> {
            String pageUrl = pageUtil.getPageUrlBusinessType(message.getBusinessType());
            if (!Objects.equals(pageUrl, "")) {
                message.setPageUrl(pageUrl);
            }
        });
        responseDto.setList(list);
        responseDto.setTotal(total);
        return responseDto;
    }

    @PubAction(value = "/setToDoneOrReadById", method = RequestMethod.POST)
    public MessageSetReadResponseDto setToDoneOrReadById(@RequestBody SetToDoneOrReadRequestDto dto) {
        return remoteSystemMessageService.SetToDoneOrReadById(UUID.fromString(dto.getId()));
    }
}
